<?

$header = new SectionTemplate;
$header->file = "header.php";
$footer = new SectionTemplate;
$footer->file = "footer.php";

$search = new SectionTemplate;
$search->file = "shows_search.php";

include_class('shows');
include_class('venues');
include_class('locations');
include_class('m2');
include_class('tours');
include_class('band_members');


$view = '';
if ($_GET['id']) {
	$bs = Show::get($_GET['id']);
	if ($_GET['media_instance_id'] > 0) { 
		$mi = MediaInstance::get($_GET['media_instance_id']);
		if (!db::isError($mi) && !db::isError($bs)) {
			if (($mi->getAreaID() == $bs->getAVAreaID() || $mi->getAreaID() == $bs->getPhotoAreaID()) && $bs->isActive()) {
				$view = 'media';
				$media = new SectionTemplate;
				$media->file = "media_detail.php";
				$media->args['media'] = $mi;
				$media->args['exitURL'] = (USE_MOD_REWRITE) ? '/show/' . $bs->getID() . '/' : '/shows.php?id=' . $bs->getID();
			}
		}
	}

	if ($view != 'media' && !db::isError($bs)) {
		$view = 'detail';
		$detail = new SectionTemplate;
		$detail->file = "show_detail.php";
		$detail->args['show'] = $bs;
		if (USE_MOD_REWRITE) {
			$auxLink = '<a href="' . SITE_WEB_DIRECTORY . '/shows/">Return to Show Calendar/Search</a>';
		} else {
			$auxLink = '<a href="' . SITE_WEB_DIRECTORY . '/shows.php">Return to Show Calendar/Search</a>';
		}
	}
}

if (!$view && $_GET['submit'] == 'search') {
	$view = 'search';
	$sl = new ShowList();
	$sl->toSearch = $_GET['toSearch'];
	$sl->includeAV = $_GET['includeAV'];
	$sl->includePictures = $_GET['includePictures'];
	$sl->isAllAges = $_GET['isAllAges'];
	$sl->keywords = $_GET['keywords'];
	$results = $sl->get($_GET['e'], $_GET['s']);
	$total = $sl->getTotal();
	$shows = array();
	foreach ($results as $sh) {
		$detail = new SectionTemplate;
		$detail->file = "show_summary.php";
		$detail->args['show'] = $sh;
		$shows[] = $detail;
	}
	if (count($shows) == 0) {
		unset($shows);
	}
	$paging = new SectionTemplate;
	$paging->file = "paging.php";
	$paging->args['total'] = $total;
	if (USE_MOD_REWRITE) {
		$paging->args['url'] = SITE_WEB_DIRECTORY . '/shows/';
	} else {
		$paging->args['url'] = SITE_WEB_DIRECTORY . '/shows.php?q=' . $_GET['q'];
	}
	$paging->args['perPage'] = 10;
	$contentTitle = "Search Results";
	if (USE_MOD_REWRITE) {
		$auxLink = '<a href="' . SITE_WEB_DIRECTORY . '/shows/">Return to Show Calendar</a>';
	} else {
		$auxLink = '<a href="' . SITE_WEB_DIRECTORY . '/shows.php">Return to Show Calendar</a>';
	}


} else if (!$view) {
	$detail = new SectionTemplate;
	$view = 'calendar';
	$detail->file = "show_calendar.php";
	$contentTitle = "Show Calendar";
}


